<template>
  <div class="gi_page">
    <SplitPanel>
      <template #left>
        <CategoryTree @node-click="handleSelectCategory" />
      </template>
      <template #main>
        <GiTable
          row-key="id" :data="dataList" :columns="columns" :loading="loading"
          :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
          :pagination="pagination" :disabled-column-keys="['name']"
          @refresh="search"
        >
          <template #toolbar-left>
            <a-input v-model="queryForm.title" placeholder="请输入关键词" allow-clear @change="search">
              <template #prefix><icon-search /></template>
            </a-input>
            <a-select v-model="queryForm.action" placeholder="请选择方法" allow-clear style="width: 150px" @change="search">
              <a-option>GET</a-option>
              <a-option>POST</a-option>
              <a-option>PUT</a-option>
              <a-option>DELETE</a-option>
              <a-option>PATCH</a-option>
            </a-select>
            <a-button @click="reset">重置</a-button>
          </template>
          <template #toolbar-right>
            <a-button v-permission="['system:api:add']" type="primary" @click="onAdd">
              <template #icon><icon-plus /></template>
              <span>新增</span>
            </a-button>
          </template>
          <template #title="{ record }">
            <span v-if="record.type === 'SYS' && record.title !== ''">
              <a-tag color="green">{{ `[${record.type}] ${record.title}` }}</a-tag>
            </span>
            <span v-if="record.type !== 'SYS' && record.title !== ''">
              <a-tag color="blue">{{ `[${record.type}] ${record.title}` }}</a-tag>
            </span>
            <span v-if="record.title === ''"><a-tag color="red">暂无</a-tag> </span>
          </template>
          <template #path="{ record }">
            <a-popover title="接口详情" position="top">
              <span>
                <a-tag v-if="record.action === 'GET'" color="blue">{{ record.action }}</a-tag>
                <a-tag v-if="record.action === 'POST'" color="green">{{ record.action }}</a-tag>
                <a-tag v-if="record.action === 'PUT'" color="orangered">{{ record.action }}</a-tag>
                <a-tag v-if="record.action === 'DELETE'" color="red">{{ record.action }}</a-tag>
                <a-tag v-if="record.action === 'PATCH'" color="red">{{ record.action }}</a-tag>
                {{ record.path }}
              </span>
              <template #content>
                <p>
                  <span v-if="record.type === 'SYS' && record.title !== ''"><a-tag color="green">
                    {{ `[${record.type}] ${record.title}` }}
                  </a-tag>
                  </span>
                  <span v-if="record.type !== 'SYS' && record.title !== ''">
                    <a-tag color="orangered">{{ `[${record.type}] ${record.title}` }} </a-tag>
                  </span>
                  <span v-if="record.title === ''"><a-tag color="red">暂无</a-tag> </span>
                </p>
                <p>Handle: {{ record.handle }}</p>
                <p>
                  Method:
                  <a-tag v-if="record.action === 'GET'" color="blue">{{ record.action }}</a-tag>
                  <a-tag v-if="record.action === 'POST'" color="green">{{ record.action }}</a-tag>
                  <a-tag v-if="record.action === 'PUT'" color="orangered">{{ record.action }}</a-tag>
                  <a-tag v-if="record.action === 'DELETE'" color="red">{{ record.action }}</a-tag>
                  <a-tag v-if="record.action === 'PATCH'" color="red">{{ record.action }}</a-tag>
                </p>
                <p>接口类型: {{ record.type }}</p>
              </template>
            </a-popover>
          </template>
          <template #action="{ record }">
            <a-space>
              <a-link v-permission="['system:api:update']" @click="onUpdate(record)">修改</a-link>
              <a-link
                v-permission="['system:api:delete']" status="danger"
                :title="record.type === 'SYS' ? '系统内置数据不能删除' : undefined" :disabled="record.type === 'SYS'"
                @click="onDelete(record)"
              >
                删除
              </a-link>
            </a-space>
          </template>
        </GiTable>

        <ApiAddModal ref="ApiAddModalRef" @save-success="search" />
      </template>
    </SplitPanel>
  </div>
</template>

<script setup lang="ts">
import { Message } from '@arco-design/web-vue'
import { load } from '@amap/amap-jsapi-loader'
import ApiAddModal from './ApiAddModal.vue'
import CategoryTree from './tree/index.vue'
import { type DataRecord, deleteApi, exportApi, listApi } from '@/apis/system/api'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useDownload, useTable } from '@/hooks'
import { isMobile } from '@/utils'

import has from '@/utils/has'

defineOptions({ name: 'SysApi' })

const queryForm = reactive({
  title: undefined,
  action: undefined,
  category: undefined,
  sort: ['createTime,desc'],
})
const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete,
} = useTable((p) => listApi({ ...queryForm, page: p.page, size: p.size }), { immediate: false })

loading.value = true

const columns: TableInstanceColumns[] = [
  {
    title: '序号',
    width: 66,
    align: 'center',
    render: ({ rowIndex }) => h('span', {}, rowIndex + 1 + (pagination.current - 1) * pagination.pageSize),
  },
  { title: '名称', dataIndex: 'title', slotName: 'title', ellipsis: true, tooltip: true },
  { title: '路径', dataIndex: 'path', slotName: 'path', ellipsis: true, tooltip: true },
  { title: '创建人', dataIndex: 'createUserString', ellipsis: true, tooltip: true, show: true, width: 140 },
  { title: '创建时间', dataIndex: 'createTime', width: 180 },
  { title: '修改人', dataIndex: 'updateUserString', width: 140, ellipsis: true, tooltip: true, show: false },
  { title: '修改时间', dataIndex: 'updatedTime', width: 180, show: false },
  {
    title: '操作',
    slotName: 'action',
    width: 200,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['system:api:update', 'system:api:delete']),
  },
]

// 重置
const reset = () => {
  queryForm.title = undefined
  queryForm.action = undefined
  queryForm.category = undefined
  search()
}

// 删除
const onDelete = (item: DataRecord) => {
  return handleDelete(() => deleteApi(item.id), { content: `是否确定删除接口 [${item.title}]？`, showModal: true })
}

const ApiAddModalRef = ref<InstanceType<typeof ApiAddModal>>()
// 新增
const onAdd = () => {
  ApiAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (item: DataRecord) => {
  ApiAddModalRef.value?.onUpdate(item.id)
}
const handleSelectCategory = (keys: Array<any>) => {
  queryForm.category = keys.length === 1 ? keys[0] : undefined
  search()
}
</script>

<style lang="scss" scoped></style>
